package org.xx.sipahi.jpa;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.xx.sipahi.Notice;

import static org.xx.sipahi.jpa.CategoryRepository.toCategory;

public interface NoticeRepository
        extends JpaRepository<NoticePO, Long>, JpaSpecificationExecutor<NoticePO> {
    static Notice toNotice(
            NoticePO po
    ) {
        if (po == null) {
            return null;
        } else {
            return new Notice(
                    po.getId(),
                    po.getTitle(),
                    toCategory(po.getCategory()),
                    po.getKeywords(),
                    po.getBrief(),
                    po.getCreator(),
                    po.getCreateTime(),
                    po.getUpdater(),
                    po.getLastModifiedTime(),
                    po.getStatus(),
                    po.isStar(),
                    po.getStickTime(),
                    ""
            );
        }
    }

    static Notice toNotice(
            NoticePO po,
            NoticeContentPO cpo
    ) {
        if (po == null || cpo == null) {
            return null;
        } else {
            return new Notice(
                    po.getId(),
                    po.getTitle(),
                    toCategory(po.getCategory()),
                    po.getKeywords(),
                    po.getBrief(),
                    po.getCreator(),
                    po.getCreateTime(),
                    po.getUpdater(),
                    po.getLastModifiedTime(),
                    po.getStatus(),
                    po.isStar(),
                    po.getStickTime(),
                    cpo.getContent()
            );
        }
    }
}
